package com.lanou3g.Code0517.study.Hw;



import com.lanou3g.Code0517.study.Hw.common.Constant;
import com.lanou3g.Code0517.study.Hw.controller.Controller;
import com.lanou3g.Code0517.study.Hw.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet(name = "LoginServlet", urlPatterns = "/Login")
public class LoginServlet extends HttpServlet {
    private Controller controller = new Controller();
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String username = request.getParameter("username");
        String password = request.getParameter("pwd");
        String week = request.getParameter("week");



        Controller controller = new Controller();
        User user = controller.login(username, password);


          String msg ="";
        if (user != null) {


                HttpSession session =request.getSession();
                user.setPsd("");
                session.setAttribute(Constant.Session.USER,user);
                if (week!=null){
                    String sessionId = session.getId();
                    Cookie cookie = new Cookie("JSESIONID",sessionId);
                    cookie.setMaxAge(7*24*3600);
                    cookie.setDomain("/");
                    response.addCookie(cookie);
                }

                //request.getRequestDispatcher("/page.jsp").forward(request,response);
              response.sendRedirect("/Hw/page.jsp");

        } else {
            msg="登录失败5秒后自动转到该页面";
            request.getRequestDispatcher("/Hw/earch.jsp").forward(request,response);
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);

    }
}
